﻿using Microsoft.AspNetCore.Mvc.ViewEngines;

namespace SimpleInOutControl.Services
{
    /// <summary>
    /// 基础服务
    /// </summary>
    public class BaseService
    {
        /// <summary>
        /// 获取排序字符串（所有字段相同排序方式）
        /// </summary>
        /// <param name="sortColumns">排序字段，多字段用“,”分隔</param>
        /// <param name="isAsc">是否升序</param>
        /// <returns></returns>
        public string GetOrderString(string sortColumns, bool isAsc = true)
        {
            if (!string.IsNullOrEmpty(sortColumns))
            {
                List<string> lsOrders = new List<string>();
                var orders = sortColumns.Split(',').ToList();

                orders.ForEach(u =>
                {
                    var orderType = isAsc ? "asc" : "desc";
                    if (!string.IsNullOrEmpty(u.Trim()))
                    {
                        lsOrders.Add($@"{u} {orderType}");
                    }
                });
                return lsOrders.Count <= 0 ? "" : string.Join(", ", lsOrders);
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 获取排序字符串（所有字段可单独控制排序方式）
        /// </summary>
        /// <param name="sortColumns">排序字段</param>
        /// <returns></returns>
        public string GetOrderString(Dictionary<string, bool> sortColumns)
        {
            List<string> lsOrders = new List<string>();
            foreach (var item in sortColumns)
            {
                var orderType = item.Value ? "asc" : "desc";
                lsOrders.Add($@"{item.Key} {orderType}");
            }
            return lsOrders.Count <= 0 ? "" : string.Join(", ", lsOrders);
        }
    }
}
